package simulation; /**
 * Copyright 2011 Jonathan Haas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.concurrent.CopyOnWriteArrayList;

public class Pin {
    protected final int _x;
    protected final int _y;
    private boolean _powered;
    private CopyOnWriteArrayList<PoweredListener> _poweredListeners = new CopyOnWriteArrayList<PoweredListener>();
    private boolean _inverted;

    public boolean isPowered() {
        return _powered ^ _inverted;
    }

    public Pin(int y, int x) {
        _y = y;
        _x = x;
    }

    public int getX() {
        return _x;
    }

    public int getY() {
        return _y;
    }

    public void setPowered(boolean powered) {
        if (powered == _powered) return;
        this._powered = powered;
        for (PoweredListener listener : _poweredListeners) {
            listener.changedPower();
        }
    }


    public void setInverted(final boolean inverted) {
        _inverted = inverted;
    }

    public boolean isInverted() {
        return _inverted;
    }

    public void addPoweredListener(PoweredListener poweredListener) {
        _poweredListeners.add(poweredListener);
    }

    public void removePoweredListener(PoweredListener poweredListener) {
        _poweredListeners.remove(poweredListener);
    }

    public static void draw(final Graphics2D g2d, final Pin pin, double radius) {
        if(pin.isInverted()){
            radius *= 2.5;
            g2d.fill(new Ellipse2D.Double(pin.getX() - radius, pin.getY() - radius, radius + radius, radius + radius));
        }
        else{
//            g2d.draw(new Ellipse2D.Double(pin.getX() - radius, pin.getY() - radius, radius + radius, radius + radius));
        }
    }
}
